package com.qss.service.impl;

import com.qs.mapper.SysDeptMapper;
import com.qs.pojo.SysDept;
import com.qs.pojo.SysDeptExample;
import com.qss.service.DeptService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

@Service(interfaceClass = DeptService.class,timeout = 10000)
public class DeptServiceImpl implements DeptService {
    @Autowired
    private SysDeptMapper mapper;
    @Override
    public List<SysDept> queryDepts(SysDept dept) {

        SysDeptExample example = new SysDeptExample();
        SysDeptExample.Criteria criteria = example.createCriteria();
        if(dept!=null){
            if(!StringUtils.isEmpty(dept.getDeptName())){
                criteria.andDeptNameLike("%"+dept.getDeptName()+"%");
            }
            if(!StringUtils.isEmpty(dept.getStatus())){
                criteria.andStatusEqualTo(dept.getStatus());
            }

        }

        List<SysDept> sysDepts = mapper.selectByExample(example);
        return sysDepts;
    }

    @Override
    public SysDept queryUserById(Integer deptId) {

        return mapper.selectByPrimaryKey(deptId);
    }

    @Override
    public boolean saveUser(SysDept dept) {
        if (dept.getDeptId()!=null){
            int flag=mapper.updateByPrimaryKeySelective(dept);
            return flag==1?true:false;
        }
        int flag=mapper.insertSelective(dept);
        return flag==1?true:false;
    }

    @Override
    public boolean delUserById(Integer[] ids) {
        boolean status=false;
        int num=0;
        for (Integer id:ids){
            num+=mapper.deleteByPrimaryKey(id);
        }
        return num==ids.length?true:false;
    }

    @Override
    public SysDept queryDeptByName(String deptName) {
        SysDeptExample ex = new SysDeptExample();
        SysDeptExample.Criteria criteria = ex.createCriteria();
        criteria.andDeptNameEqualTo(deptName);
        List<SysDept> users = mapper.selectByExample(ex);
        if (users==null ||users.size()==0){
            return null;

        }
        return users.get(0);
    }
}
